<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="真实姓名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="真实姓名"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="userUsername">
      <el-input v-model="dataForm.userUsername" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="userPassowrd">
      <el-input v-model="dataForm.userPassowrd" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="角色" prop="userRole">
      <el-input v-model="dataForm.userRole" placeholder="角色"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="userCreatetime">
      <el-input v-model="dataForm.userCreatetime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="userPhone">
      <el-input v-model="dataForm.userPhone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="身份证号" prop="userCarid">
      <el-input v-model="dataForm.userCarid" placeholder="身份证号"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="userSex">
      <el-input v-model="dataForm.userSex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="userImg">
      <el-input v-model="dataForm.userImg" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="身份证照片" prop="userCardimg">
      <el-input v-model="dataForm.userCardimg" placeholder="身份证照片"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="userEmail">
      <el-input v-model="dataForm.userEmail" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="userState">
      <el-input v-model="dataForm.userState" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="用户对应角色id" prop="userRoled">
      <el-input v-model="dataForm.userRoled" placeholder="用户对应角色id"></el-input>
    </el-form-item>
    <el-form-item label="余额" prop="userMoney">
      <el-input v-model="dataForm.userMoney" placeholder="余额"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: 0,
          userName: '',
          userUsername: '',
          userPassowrd: '',
          userRole: '',
          userCreatetime: '',
          userPhone: '',
          userCarid: '',
          userSex: '',
          userImg: '',
          userCardimg: '',
          userEmail: '',
          userState: '',
          userRoled: '',
          userMoney: ''
        },
        dataRule: {
          userName: [
            { required: true, message: '真实姓名不能为空', trigger: 'blur' }
          ],
          userUsername: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          userPassowrd: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          userRole: [
            { required: true, message: '角色不能为空', trigger: 'blur' }
          ],
          userCreatetime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          userPhone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          userCarid: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' }
          ],
          userSex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          userImg: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          userCardimg: [
            { required: true, message: '身份证照片不能为空', trigger: 'blur' }
          ],
          userEmail: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          userState: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          userRoled: [
            { required: true, message: '用户对应角色id不能为空', trigger: 'blur' }
          ],
          userMoney: [
            { required: true, message: '余额不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.userId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/users/user/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userName = data.user.userName
                this.dataForm.userUsername = data.user.userUsername
                this.dataForm.userPassowrd = data.user.userPassowrd
                this.dataForm.userRole = data.user.userRole
                this.dataForm.userCreatetime = data.user.userCreatetime
                this.dataForm.userPhone = data.user.userPhone
                this.dataForm.userCarid = data.user.userCarid
                this.dataForm.userSex = data.user.userSex
                this.dataForm.userImg = data.user.userImg
                this.dataForm.userCardimg = data.user.userCardimg
                this.dataForm.userEmail = data.user.userEmail
                this.dataForm.userState = data.user.userState
                this.dataForm.userRoled = data.user.userRoled
                this.dataForm.userMoney = data.user.userMoney
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/users/user/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'userName': this.dataForm.userName,
                'userUsername': this.dataForm.userUsername,
                'userPassowrd': this.dataForm.userPassowrd,
                'userRole': this.dataForm.userRole,
                'userCreatetime': this.dataForm.userCreatetime,
                'userPhone': this.dataForm.userPhone,
                'userCarid': this.dataForm.userCarid,
                'userSex': this.dataForm.userSex,
                'userImg': this.dataForm.userImg,
                'userCardimg': this.dataForm.userCardimg,
                'userEmail': this.dataForm.userEmail,
                'userState': this.dataForm.userState,
                'userRoled': this.dataForm.userRoled,
                'userMoney': this.dataForm.userMoney
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
